package com.neutech.maplestory.client;

import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.MapleStoryFrame;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 游戏的主入口文件
 *
 * ·@author：冯。
 * ·@version：v.1.0。
 * ·@docroot：表示产生文档的根路径。
 * ·@param：方法的参数类型。
 * ·@return：方法的返回类型。
 */
public class MapleStoryClient extends MapleStoryFrame {
    public Hero hero = new Hero(this,200,300);
    //public Arrow arrow = new Arrow(400,500,hero.dir);
    public List<Arrow> arrows = new CopyOnWriteArrayList<>();
    public List<Mob> mobs = new CopyOnWriteArrayList<>();
    public List<Power> powers = new CopyOnWriteArrayList<>();
    /**
     * 掉落的道具的容器，还没进入到道具包内
     */
    public List<Item> items = new CopyOnWriteArrayList<>();
    public ItemPackage itemPackage = new ItemPackage(this);
    //public Random r = new Random();
    //public Item item = new Item(this,450,570,r.nextInt(4));

    {
        for (int i = 0; i < 3; i++) {
            Mob mob = new Mob(this,500 + (i * 80),300,3,Direction.RIGHT);
            mobs.add(mob);
        }
        for (int i = 0; i < 3; i++) {
            Mob mob = new Mob(this,1000 + (i * 80),300,3,Direction.LEFT);
            mobs.add(mob);
        }
        for (int i = 0; i < 3; i++) {
            Mob mob = new Mob(this,350 + (i * 290),100,3,Direction.LEFT,Action.STAND);
            mobs.add(mob);
        }
       /* Mob mob = new Mob(this,500,650 - 117,3);
        mobs.add(mob);*/
    }

    public List<Base> bases = new CopyOnWriteArrayList<>();
    /*{
        Base base01 = new Base(this,0,750,200,250);
        bases.add(base01);
        Base base02 = new Base(this,200,700,200,300);
        bases.add(base02);
        Base base03 = new Base(this,400,650,400,350);
        bases.add(base03);
        Base base04 = new Base(this,800,700,1100,300);
        bases.add(base04);
    }*/
    //前景
        Base tree01 = new Base(this,39,838 - 392 + 10,"tree01");
        Base tree02 = new Base(this,655,698 - 153 + 13,"tree02");
        Base tree03 = new Base(this,795,698- 315 + 13,"tree03");
        Base tree04 = new Base(this,795 + 310,698 - 153 + 13,"tree02");
        Base tree05 = new Base(this,1264,698 - 310 + 13,"tree04");
        Base tree06 = new Base(this,1486,771 - 411 + 10,"tree05");
    //梯子
        public Base ladder = new Base(this,570 + 150,295,"ladder");
    //云 天空
        public Base yun = new Base(this,0,0,"yun");
    {


        //bases.add(yun);
        //背景
        Base bg = new Base(this,0,0,"bg");
        bases.add(bg);
        /*Base bg2 = new Base(this,0,0,"bg2");
        bases.add(bg2);*/
        //地基
        Base base01 = new Base(this,0,838,"base01");
        bases.add(base01);
        Base base02 = new Base(this,517,767,"base02");
        bases.add(base02);
        Base base03 = new Base(this,627,698,"base03");
        bases.add(base03);
        Base base04 = new Base(this,842,698,"base04");
        bases.add(base04);
        Base base05 = new Base(this,1062,698,"base05");
        bases.add(base05);
        Base base06 = new Base(this,1284,698,"base06");
        bases.add(base06);
        Base base07 = new Base(this,1510,771,"base07");
        bases.add(base07);
        //跳台
        Base smallPlatform01 = new Base(this,350,615,"smallPlatform");
        bases.add(smallPlatform01);
        Base platform = new Base(this,650,535,"platform");
        bases.add(platform);
        Base smallPlatform02 = new Base(this,1150,455,"smallPlatform");
        bases.add(smallPlatform02);
        for (int i = 0; i < 3; i++) {
            Base smallPlatform = new Base(this,850 - (i * 290),375 - (i *80),"smallPlatform");
            bases.add(smallPlatform);
        }
    }

    @Override
    public void init() {
        super.init();
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_I){
                    itemPackage.live =  !itemPackage.live;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        tree01.draw(g);
        tree02.draw(g);
        tree03.draw(g);
        tree04.draw(g);
        tree05.draw(g);
        tree06.draw(g);

        yun.draw(g);
        Iterator<Base> itBases = bases.iterator();
        while (itBases.hasNext()){
            Base base = itBases.next();
            base.draw(g);
        }

        ladder.draw(g);

        hero.draw(g);
        //使用迭代器和CopyOnWriteArrayList<>();解决数组索引移动（非线程安全的问题）
        Iterator<Arrow> itArrows = arrows.iterator();
        while (itArrows.hasNext()){
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);
        }
/*        for (int i = 0; i < arrows.size(); i++) {
            Arrow arrow = arrows.get(i);
            arrow.draw(g);
            arrow.hit(mobs);
        }*/
        Iterator<Mob> itMobs = mobs.iterator();
        while (itMobs.hasNext()){
           Mob mob = itMobs.next();
           mob.draw(g);
        }
/*        for (int i = 0; i < mobs.size(); i++) {
            mobs.get(i).draw(g);
        }*/

        //画出每一个数字
        Iterator<Power> itPowers = powers.iterator();
        while (itPowers.hasNext()){
            Power power = itPowers.next();
            power.draw(g);
        }

        //有迭代器循环调用，画出每一个道具
        Iterator<Item> itItems = items.iterator();
        while (itItems.hasNext()){
            Item item = itItems.next();
            item.draw(g);
        }
        //道具包
        itemPackage.draw(g);

        Font f = g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,24));
        g.drawString("人物的方向：" + hero.dir,100,100);
        g.drawString("人物的动作：" + hero.action,100,150);
        g.drawString("HP：" + hero.HP,100,200);
        g.drawString("MP：" + hero.MP,100,250);
        g.drawString("EXP：" + hero.EXP,100,300);
        g.drawString("LV：" + hero.LV,100,350);
        g.setFont(f);
    }

    public static void main(String[] args) {
        new MapleStoryClient().init();
    }


}
